﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.SY
{
    public partial class UserPemsissionRMFrm : Form
    {
        #region
        public int user_ID = 0;
        public string user_Name;
        private string roles_idStr = "";
        private string group_idStr = "";
        PublicDataSet bll_CommonPub = new PublicDataSet();
        TUsers_BLL bll_TUsers = new TUsers_BLL(); 
        TUserPermissionRelation_BLL bll_TUserPermissionRelation = new TUserPermissionRelation_BLL();
        public UserPemsissionRMFrm()
        {
            InitializeComponent();
        }
        private void UserPemsissionRMFrm_Load(object sender, EventArgs e)
        { 
            this.DBind();
        }
        #endregion
          
        #region DBind()
        public void DBind()
        {
            this.DBind_Organization();
            this.DBind_Roles();
            this.DBind_Group();
            this.DBind_Pemsission();
            this.DBind_MenuMoudle();
        }
        #endregion

        #region 绑定组织用户信息
        public void DBind_Organization()
        {
            this.treeViewUserTOrganization.Nodes.Clear();
            DataSet ds = new DataSet();
            ds = bll_CommonPub.GetDataSet("TOrganization", "");
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv["Organization_name"].ToString();
                node.Name = drv["Organization_id"].ToString();
                treeViewUserTOrganization.Nodes.Add(node);
                this.AddNodes_Organization(dt, node);
            }
            this.treeViewUserTOrganization.ExpandAll();
            this.treeViewUserTOrganization.SelectedNode = treeViewUserTOrganization.TopNode; //最顶端节点选中
        }
        public void AddNodes_Organization(DataTable dt, TreeNode node)
        {
            DataSet ds_user = new DataSet();
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id='" + node.Name + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row["Organization_name"].ToString();
                replyNode.Name = row["Organization_id"].ToString();
                node.Nodes.Add(replyNode);
                ds_user = null;
                ds_user = bll_TUsers.GetList_TUsersByO_id(Convert.ToInt32(replyNode.Name));
                if (ds_user != null)
                {
                    DataView dv1 = new DataView(ds_user.Tables[0]);
                    foreach (DataRowView row1 in dv1)
                    {
                        TreeNode replyNode1 = new TreeNode();
                        replyNode1.Text = row1["Username"].ToString();
                        replyNode1.Name = row1["User_id"].ToString();
                        replyNode.Nodes.Add(replyNode1);
                    }
                }
                this.AddNodes_Organization(dt, replyNode);
            }
        }

        private void labExpansion_Click(object sender, EventArgs e)
        {
            this.treeViewUserTOrganization.ExpandAll();
        }

        private void labFold_Click(object sender, EventArgs e)
        {
            this.treeViewUserTOrganization.CollapseAll();
        }
        #endregion

        #region 绑定角色
        public void DBind_Roles()
        {
            this.treeViewUserRoles.Nodes.Clear();
            DataSet ds = new DataSet();
            ds = bll_CommonPub.GetDataSet("TRoles", "");
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv["Role_name"].ToString();
                node.Name = drv["Role_id"].ToString();
                this.treeViewUserRoles.Nodes.Add(node);
                this.AddNodes_Roles(dt, node);
            }
            this.treeViewUserRoles.ExpandAll();
            this.treeViewUserRoles.SelectedNode = this.treeViewUserRoles.TopNode; //最顶端节点选中
        }
        public void AddNodes_Roles(DataTable dt, TreeNode node)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id='" + node.Name + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row["Role_name"].ToString();
                replyNode.Name = row["Role_id"].ToString();
                node.Nodes.Add(replyNode);
                this.AddNodes_Roles(dt, replyNode);
            }
        }
        #endregion

        #region 绑定组
        public void DBind_Group()
        {
            this.treeViewUserGroup.Nodes.Clear();
            DataSet ds = new DataSet();
            ds = bll_CommonPub.GetDataSet("TGroup", "");
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv["Group_name"].ToString();
                node.Name = drv["Group_id"].ToString();
                treeViewUserGroup.Nodes.Add(node);
                this.AddNodes_Group(dt, node);
            }
            this.treeViewUserGroup.ExpandAll();
            this.treeViewUserGroup.SelectedNode = treeViewUserGroup.TopNode; //最顶端节点选中
        }
        public void AddNodes_Group(DataTable dt, TreeNode node)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id='" + node.Name + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row["Group_name"].ToString();
                replyNode.Name = row["Group_id"].ToString();
                node.Nodes.Add(replyNode);
                this.AddNodes_Group(dt, replyNode);
            }
        }
        #endregion

        #region 绑定权限
        TSystemRegistBLL bll_SystemRegist = new TSystemRegistBLL();
        public void DBind_MenuMoudle()
        {
            this.treeViewMenuPermission.Nodes.Clear();
            DataSet ds = new DataSet();
            ds = bll_SystemRegist.GetList_SystemMeun();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "headID=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv["Menu_Name"].ToString();
                node.Name = drv["ID"].ToString();
                node.ImageIndex = 0;
                node.SelectedImageIndex = 0;
                this.treeViewMenuPermission.Nodes.Add(node);
                this.AddNodes(dt, node);
            }
            this.treeViewMenuPermission.Nodes[0].Expand();
        }

        /// <summary>
        /// AddNodes
        /// </summary>
        /// <param name="dt">dt</param>
        /// <param name="node">node</param>
        public void AddNodes(DataTable dt, TreeNode node)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "headID='" + node.Name + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row["Menu_Name"].ToString();
                replyNode.Name = row["ID"].ToString();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                node.Nodes.Add(replyNode);
                AddNodes(dt, replyNode);
            }
        }


        public void DBind_MenuMoudleAll()
        {
            this.treeViewMenuPermissionAll.Nodes.Clear();
            DataSet ds = new DataSet();
            ds = bll_SystemRegist.GetList_SystemMeun();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "headID=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv["Menu_Name"].ToString();
                node.Name = drv["ID"].ToString();
                node.ImageIndex = 0;
                node.SelectedImageIndex = 0;
                this.treeViewMenuPermissionAll.Nodes.Add(node);
                this.AddAllNodes(dt, node);
            }
            this.treeViewMenuPermissionAll.Nodes[0].Expand();
        }

        /// <summary>
        /// AddNodes
        /// </summary>
        /// <param name="dt">dt</param>
        /// <param name="node">node</param>
        public void AddAllNodes(DataTable dt, TreeNode node)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "headID='" + node.Name + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row["Menu_Name"].ToString();
                replyNode.Name = row["ID"].ToString();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                node.Nodes.Add(replyNode);
                AddAllNodes(dt, replyNode);
            }
        }
        #endregion

        #region 绑定权限列表
        private void DBind_Pemsission()
        {
            DataSet ds = new DataSet();
            ds = bll_CommonPub.GetDataSet("TDictionary_Category", " SortID=1 ");
            this.dataGridViewMoudlePermisissionList.DataSource = ds.Tables[0].DefaultView;
            this.dataGridViewMoudlePermisissionList.ClearSelection();
        }

        private void dataGridViewMoudlePermisissionList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                this.dataGridViewMoudlePermisissionList.Rows[e.RowIndex].Selected = true;
        }


        private void DBind_PemsissionAll()
        {
            DataSet ds = new DataSet();
            ds = bll_CommonPub.GetDataSet("TDictionary_Category", " SortID=1 ");
            this.dataGridViewMoudlePermisissionListAll.DataSource = ds.Tables[0].DefaultView;
            this.dataGridViewMoudlePermisissionListAll.ClearSelection();
        }
        #endregion

        #region 初始
        private void DBindClear()
        {
            this.DBind();
            this.labShowSelectUser.Text = "所选用户:";
            this.user_ID = 0;
        }
        #endregion

        #region 设置组
        #region 选中用户所属组
        public string strGroup = "";
        public void GetstrGroup()
        {
            this.strGroup = "";
            foreach (TreeNode tn in this.treeViewUserGroup.Nodes)
            {
                GetstrGroup_Nodes(tn);
            }
        }
        public void GetstrGroup_Nodes(TreeNode node)
        {
            if (node.Checked)
            {
                strGroup += node.Name + ",";
            }
            foreach (TreeNode tn in node.Nodes)
            {
                GetstrGroup_Nodes(tn);
            }
        }
        #endregion

        private void btnSaveGroup_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("编辑")) return;

            this.GetstrGroup();
            if (user_ID == 0)
            {
                MessageClass.error("请选择用户！");
                return;
            }
            if (MessageClass.enquire("确认保存！") == DialogResult.OK)
            {
                if (user_ID == 2 && SystemVariable.User_ID != 2)
                {
                    MessageClass.right("成功！");
                }
                else
                {
                    if (bll_TUserPermissionRelation.SetGroup(user_ID, strGroup))
                    {
                        MessageClass.right("成功！");
                        SystemVariable.Vaidation(SystemVariable.User_ID);
                    }
                    else
                    {
                       MessageClass.warning("失败！");
                    }
                }
            }
        }
        #endregion

        #region 设置角色

        #region  选中用户所属角色
        public string strRole = "";
        public void GetstrRole()
        {
            this.strRole = "";
            foreach (TreeNode tn in this.treeViewUserRoles.Nodes)
            {
                GetstrRole_Nodes(tn);
            }
        }
        public void GetstrRole_Nodes(TreeNode node)
        {
            if (node.Checked)
            {
                strRole += node.Name + ",";
            }
            foreach (TreeNode tn in node.Nodes)
            {
                GetstrRole_Nodes(tn);
            }
        }
        #endregion

        private void btnSaveRole_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("编辑")) return;

            this.GetstrRole();
            if (user_ID == 0)
            {
                MessageClass.error("请选择用户！");
                return;
            }
            if (MessageClass.enquire("确认保存！") == DialogResult.OK)
            {
                if (user_ID == 2 && SystemVariable.User_ID != 2)
                {
                    MessageClass.right("成功！");
                }
                else
                {
                    if (bll_TUserPermissionRelation.SetRole(user_ID, strRole))
                    {
                        MessageClass.right("成功！");
                        SystemVariable.Vaidation(SystemVariable.User_ID);

                    }
                    else
                    {
                       MessageClass.warning("失败！");
                    }
                }
            }
        }
        #endregion

        #region 设置权限
        private void btnRolePemsissionSave_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("编辑")) return;

            if (user_ID == 0) { MessageClass.error("请选择用户！"); return; }

            if (rbALLPermissions.Checked || rbDelALLPermissions.Checked)
            {
                if (rbALLPermissions.Checked)
                {
                    string str = bll_CommonPub.GetTPermissionStr();
                    if (str != "")
                        userStrPermissions = str;
                }
                if (rbDelALLPermissions.Checked)
                    userStrPermissions = "";
            }
            else
            {
                if (moudleid == 0) { MessageClass.error("请选择模块权限！"); return; }
                this.GetPermisissionStr();
            }

            if (MessageClass.enquire("确认保存！") == DialogResult.OK)
            {
                if (user_ID == 2 && SystemVariable.User_ID != 2)
                {
                    MessageClass.right("成功！");
                }
                else
                {
                    if (bll_TUserPermissionRelation.SetPermisissions(user_ID, userStrPermissions))
                    {
                        SystemVariable.AddLogInfo("权限管理", "用户ID为:【" + user_ID + "】权限设置。");

                        MessageClass.right("成功！");

                        this.GetUserPermisissionStr(user_ID);
                        SystemVariable.Vaidation(SystemVariable.User_ID);
                        if (moudleid > 0)
                            this.treeViewMenuPermissionClik(moudleid);
                        rbALLPermissions.Checked = false;
                        rbDelALLPermissions.Checked = false;
                    }
                    else
                    {
                       MessageClass.warning("失败！");
                    }
                }
            }
        }

        /// 获取选中权限 
        public void GetPermisissionStr()
        {
            this.userStrPermissions = "";
            this.strNewpers = "";
            if (strOldpers != "")
            {
                string[] arryStrOldpers = strOldpers.Split(',');
                for (int y = 0; y < arryStrOldpers.Length - 1; y++)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i] == arryStrOldpers[y])
                        {
                            list.Remove(list[i]);
                            break;
                        }
                    }
                }
            }

            for (int i = 0; i < dataGridViewMoudlePermisissionList.Rows.Count; i++)
            {
                if ((bool)dataGridViewMoudlePermisissionList.Rows[i].Cells[0].Value)
                {
                    strNewpers += dataGridViewMoudlePermisissionList.Rows[i].Cells[1].Value.ToString() + ",";
                }
            }

            if (strNewpers != "")
            {
                foreach (var item in strNewpers.Split(','))
                {
                    list.Add(item);
                }
            }
            userStrPermissions = string.Join(",", list.ToArray());
        }

        #endregion

        #region 权限事件
        private int moudleid = 0;
        private string strOldpers = "";
        private string strNewpers = "";
        private void treeViewMenuPermission_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (user_ID == 0) { MessageClass.error("未选用户！"); return; }
            rbALLPermissions.Checked = false;
            rbDelALLPermissions.Checked = false;
            this.GetUserPermisissionStr(user_ID);
            if (e.Node.LastNode == null)
            {
                this.moudleid = Convert.ToInt32(e.Node.Name);
                this.treeViewMenuPermissionClik(moudleid);
            }
        }

        public void treeViewMenuPermissionClik(int moudleid)
        {
            this.strOldpers = "";

            #region 模块对应所具有权限
            DataSet ds1 = new DataSet();
            ds1 = bll_CommonPub.GetDataSet("SELECT a.ID, '' AS sortID, b.Value FROM  dbo.TPermissions AS a LEFT OUTER JOIN dbo.TDictionary_Category AS b ON a.Type = b.ID WHERE (a.MoudleID = " + moudleid + " ) AND (a.Display = 1)");
            if (ds1 != null)
            {
                dataGridViewMoudlePermisissionList.DataSource = ds1.Tables[0].DefaultView;
                this.dataGridViewMoudlePermisissionList.ClearSelection();
            }
            else
            {
                DataSet ds2 = bll_CommonPub.GetDataSet("SELECT TOP 1 * FROM DBO.TDictionary_Category WHERE SortID=1  ");
                DataTable dt = new DataTable();
                dt = ds2.Tables[0];
                dt.Rows.Clear();
                this.dataGridViewMoudlePermisissionList.DataSource = dt;
                MessageClass.error("该模块未分配权限！");
                return;
            }
            #endregion

            #region 取值勾选
            DataSet ds3 = new DataSet();
            ds3 = bll_CommonPub.GetDataSet("TPermissions", "moudleid=" + moudleid + " AND Display = 1");
            int id = 0;
            if (ds3 != null)
            {
                for (int y = 0; y < ds3.Tables[0].Rows.Count; y++)
                {
                    id = Convert.ToInt32(ds3.Tables[0].Rows[y]["ID"].ToString());
                    if (permisissionsArry != null)
                    {
                        for (int i = 0; i < permisissionsArry.Length - 1; i++)
                        {
                            if (permisissionsArry[i] == id.ToString())
                            {
                                dataGridViewMoudlePermisissionList.Rows[y].Cells[0].Value = true;
                                strOldpers += dataGridViewMoudlePermisissionList.Rows[y].Cells[1].Value + ",";
                                break;
                            }
                            else
                            {
                                dataGridViewMoudlePermisissionList.Rows[y].Cells[0].Value = false;
                            }
                        }
                    }
                    else
                    {
                        this.dataGridViewMoudlePermisissionList.Rows[y].Cells[0].Value = false;
                    }
                }
            }
            #endregion
        }

        #endregion

        #region 用户菜单事件
        private string[] permisissionsArry = null;
        private string[] permisissionsArryAll = null;
        List<string> list;
        List<string> listAll;
        private void treeViewUserTOrganization_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            #region
            this.permisissionsArry = null;
            this.permisissionsArryAll = null;
            this.list = new List<string>();
            this.listAll = new List<string>();
            this.moudleid = 0;
            rbALLPermissions.Checked = false;
            rbDelALLPermissions.Checked = false;

            this.labShowSelectUser.Text = "所选用户:";
            if (e.Node.Nodes.Count < 0) return;
            int uid = Convert.ToInt32(e.Node.Name);
            string uName = e.Node.Text;

            this.DBind_Roles();
            this.DBind_Group();
            this.DBind_Pemsission();

            this.DBind_MenuMoudleAll();
            this.DBind_PemsissionAll();

            this.roles_idStr = "";
            this.group_idStr = "";

            DataSet ds = new DataSet();
            ds = bll_TUsers.GetList_TUsersByU_id(uid, uName);
            if (ds == null) return;
            this.user_ID = uid;
            this.user_Name = uName;
            this.labShowSelectUser.Text = "所选用户:" + uName + "";
            TreeNode targetNode = new TreeNode();

            #endregion

            #region 用户所属角色
            roles_idStr = ds.Tables[0].Rows[0]["Role_id"].ToString();
            if (roles_idStr != "")
            {
                roles_idStr += ",";
                string[] rolesArryList = roles_idStr.Split(',');
                if (treeViewUserRoles.Nodes.Count > 0)
                {
                    for (int y = 0; y < rolesArryList.Length; y++)
                    {
                        foreach (TreeNode td in treeViewUserRoles.Nodes)
                        {
                            targetNode = SearchNode(td, rolesArryList[y]);
                            if (targetNode != null) targetNode.Checked = true;
                        }
                    }
                }
            }
            #endregion

            #region 用户所属组
            group_idStr = ds.Tables[0].Rows[0]["Group_id"].ToString();
            if (group_idStr != "")
            {
                string[] arrGroup = group_idStr.Split(',');
                if (treeViewUserGroup.Nodes.Count > 0)
                {
                    for (int y = 0; y < arrGroup.Length; y++)
                    {
                        foreach (TreeNode td in treeViewUserGroup.Nodes)
                        {
                            targetNode = SearchNode(td, arrGroup[y]);
                            if (targetNode != null)
                                targetNode.Checked = true;
                        }
                    }
                }
            }
            #endregion

            #region 用户权限
            this.GetUserPermisissionStr(user_ID);
            #endregion

            #region 用户所有权限
            this.GetRolePermisissionStr(user_ID);

            if (permissions_RoleStr != "")
            {
                foreach (var item in permissions_RoleStr.Substring(0, permissions_RoleStr.Length - 1).Split(','))
                {
                    if (!listAll.Contains(item))
                    {
                        listAll.Add(item);
                    }
                }
            }
            this.GetGroupPermisissionStr(user_ID);
            if (permissions_GroupStr != "")
            {
                foreach (var item in permissions_GroupStr.Substring(0, permissions_GroupStr.Length - 1).Split(','))
                {
                    if (!listAll.Contains(item))
                    {
                        listAll.Add(item);
                    }
                }
            }
            if (userStrPermissions != "")
            {
                foreach (var item in userStrPermissions.Substring(0, userStrPermissions.Length - 1).Split(','))
                {
                    if (!listAll.Contains(item))
                    {
                        listAll.Add(item);
                    }
                }
            }
            permisissionsArryAll = listAll.ToArray();
            #endregion
        }

        #region  获取用户所属角色所有权限
        private string permissions_RoleStr = "";
        private void GetRolePermisissionStr(int userid)
        {
            this.permissions_RoleStr = "";
            DataSet ds1 = new DataSet();
            DataSet ds2 = new DataSet();

            ds1 = bll_CommonPub.GetDataSet("TUserRoleRelation", "User_id='" + userid + "'");
            if (ds1.Tables[0].Rows.Count > 0)
            {
                string roleid = "";
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    roleid = ds1.Tables[0].Rows[i]["Role_id"].ToString();
                    ds2 = bll_CommonPub.GetDataSet("TRolePermissionRelation", "Role_id='" + roleid + "'");
                    if (ds2.Tables[0].Rows.Count > 0)
                    {
                        permissions_RoleStr += ds2.Tables[0].Rows[0]["Permission_id"].ToString();
                    }
                }
            }
        }
        #endregion

        #region 获取用户所属组所有权限
        private string permissions_GroupStr = "";
        private void GetGroupPermisissionStr(int userid)
        {

            permissions_GroupStr = "";
            DataSet ds1 = new DataSet();
            DataSet ds2 = new DataSet();
            DataSet ds3 = new DataSet();
            DataSet ds4 = new DataSet();
            ds1 = bll_CommonPub.GetDataSet("TUserGroupRelation", "User_id='" + userid + "'");
            if (ds1.Tables[0].Rows.Count > 0)
            {
                string groupid = "";
                string roleid = "";
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    groupid = ds1.Tables[0].Rows[i]["Group_id"].ToString();

                    ds2 = bll_CommonPub.GetDataSet("TGroupPermissionRelation", "Group_id='" + groupid + "'");
                    if (ds2.Tables[0].Rows.Count > 0)
                    {
                        this.permissions_GroupStr += ds2.Tables[0].Rows[0]["Permission_id"].ToString();
                    }

                    ds3 = bll_CommonPub.GetDataSet("TGroupRoleRelation", "Group_id='" + groupid + "'");
                    if (ds3.Tables[0].Rows.Count > 0)
                    {
                        for (int y = 0; y < ds3.Tables[0].Rows.Count; y++)
                        {
                            roleid = ds3.Tables[0].Rows[y]["Role_id"].ToString();

                            ds4 = bll_CommonPub.GetDataSet("TRolePermissionRelation", "Role_id='" + roleid + "'");
                            if (ds4.Tables[0].Rows.Count > 0)
                            {
                                for (int h = 0; h < ds4.Tables[0].Rows.Count; h++)
                                {
                                    this.permissions_GroupStr += ds4.Tables[0].Rows[0]["Permission_id"].ToString();
                                }
                            }
                        }
                    }
                }
            }
        }
        #endregion

        #region 获取用户权限，List
        private string userStrPermissions = "";
        private void GetUserPermisissionStr(int userid)
        {
            list = new List<string>();
            permisissionsArry = null;
            userStrPermissions = "";
            DataSet ds_Pemsission = new DataSet();
            ds_Pemsission = bll_CommonPub.GetDataSet("TUserPermissionRelation", "User_id='" + userid + "'");
            if (ds_Pemsission.Tables[0].Rows.Count == 0) return;
            userStrPermissions = ds_Pemsission.Tables[0].Rows[0]["Permission_id"].ToString();
            if (userStrPermissions != "")
            {
                permisissionsArry = userStrPermissions.Split(',');
                foreach (var item in userStrPermissions.Substring(0, userStrPermissions.Length - 1).Split(','))
                {
                    list.Add(item);
                }
            }
        }
        #endregion

        #region 角色组获取节点
        private TreeNode SearchNode(TreeNode td, string nodetxt)
        {
            if (td.Name == nodetxt)
            {
                return td;
            }
            TreeNode targetNode = null;
            foreach (TreeNode childNodes in td.Nodes)
            {
                targetNode = SearchNode(childNodes, nodetxt);
                if (targetNode != null)
                    break;
            }
            return targetNode;
        }
        #endregion

        #endregion

        #region 显示用户所有权限
        private void treeViewMenuPermissionAll_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (user_ID == 0) { MessageClass.error("未选用户！"); return; }
            if (e.Node.LastNode == null)
            {
                #region 模块对应所具有权限
                DataSet ds1 = new DataSet();
                ds1 = bll_CommonPub.GetDataSet("SELECT a.ID, '' AS sortID, b.Value FROM  dbo.TPermissions AS a LEFT OUTER JOIN dbo.TDictionary_Category AS b ON a.Type = b.ID WHERE (a.MoudleID = " + e.Node.Name + " ) AND (a.Display = 1)");
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    dataGridViewMoudlePermisissionListAll.DataSource = ds1.Tables[0].DefaultView;
                    this.dataGridViewMoudlePermisissionListAll.ClearSelection();
                }
                else
                {
                    DataSet ds2 = bll_CommonPub.GetDataSet("SELECT TOP 1 * FROM DBO.TDictionary_Category WHERE SortID=1  ");
                    DataTable dt = new DataTable();
                    dt = ds2.Tables[0];
                    dt.Rows.Clear();
                    this.dataGridViewMoudlePermisissionListAll.DataSource = dt;
                    MessageClass.error("该模块未分配权限！");
                    return;
                }
                #endregion

                #region 取值勾选
                DataSet ds3 = new DataSet();
                ds3 = bll_CommonPub.GetDataSet("TPermissions", "moudleid=" + e.Node.Name + " AND Display = 1");
                int id = 0;
                if (ds3.Tables[0].Rows.Count > 0)
                {
                    for (int y = 0; y < ds3.Tables[0].Rows.Count; y++)
                    {
                        id = Convert.ToInt32(ds3.Tables[0].Rows[y]["ID"].ToString());
                        if (permisissionsArryAll != null)
                        {
                            for (int i = 0; i < permisissionsArryAll.Length - 1; i++)
                            {
                                if (permisissionsArryAll[i] == id.ToString())
                                {
                                    dataGridViewMoudlePermisissionListAll.Rows[y].Cells[0].Value = true;
                                    strOldpers += dataGridViewMoudlePermisissionListAll.Rows[y].Cells[1].Value + ",";
                                    break;
                                }
                                else
                                {
                                    dataGridViewMoudlePermisissionListAll.Rows[y].Cells[0].Value = false;
                                }
                            }
                        }
                        else
                        {
                            this.dataGridViewMoudlePermisissionListAll.Rows[y].Cells[0].Value = false;
                        }
                    }
                }
                #endregion
            }
        }
        #endregion

        #region 退出
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion
    }
}
